package com.aisile.admin.controller.v1;

import com.aisile.admin.service.AdChannelService;
import com.aisile.apis.admin.AdChannelControllerApi;
import com.aisile.model.admin.dtos.ChannelDto;
import com.aisile.model.admin.pojos.AdChannel;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.utils.threadlocal.AdminThreadLocalUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Base64;
import java.util.List;

/**
 * 软件更新 禁止删除代码 只做新增不做修改
 *
 * @Auther:yry
 * @Date:2023/12/20 0020
 * @VERSON:1.0
 */
@RestController
@RequestMapping("/api/v1/adchannel")

public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    private AdChannelService adChannelService;

    @PostMapping("/upload")
    public ResponseResult uploadFile(@RequestPart("file") MultipartFile file){

        try (
                FileInputStream fis = (FileInputStream)file.getInputStream();
             ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            byte[] imageBytes = bos.toByteArray();
            String url = Base64.getEncoder().encodeToString(imageBytes);
            System.out.println(url);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ResponseResult.okResult(null);
    }



    // json
    @PostMapping("/list")
    @Override
    public ResponseResult findAllByPage(@RequestBody ChannelDto dto) {
        System.out.println(AdminThreadLocalUtils.getUser().getId());
        return adChannelService.findAllByPage(dto);
    }

    @PostMapping("/save")
    @Override
    public ResponseResult saveAdChannel(@RequestBody AdChannel adChannel) {
        return adChannelService.saveAdChannel(adChannel);
    }

    @PutMapping("/update")
    @Override
    public ResponseResult updateAdChannel(@RequestBody AdChannel adChannel) {
        return adChannelService.updateAdChannel(adChannel);
    }


    @PutMapping("/update_status/{id}/{type}")
    @Override
    public ResponseResult updateStatusByType(@PathVariable("id") Integer id,@PathVariable("type") Integer type) {
        return adChannelService.updateStatusByType(id, type);
    }

    @DeleteMapping("/delete/{id}")  // 不支持携带JSON   post  put 支持   get  delete 不支持
    @Override
    public ResponseResult deleteAdChannelById(@PathVariable("id") Integer id) {
        return adChannelService.deleteAdChannelById(id);
    }

    @GetMapping("/listbystatus")
    @Override
    public ResponseResult findAll() {
        // 1 有效
        List<AdChannel> list =  adChannelService.list(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getStatus,(short)1));
        return ResponseResult.okResult(list);
    }


}
